#pragma once
/*
*此文件由GUI工具生成
*文件功能：用于处理用户的逻辑相应代码
*功能说明：
*========================onButtonClick_XXXX
当页面中的按键按下后系统会调用对应的函数，XXX代表GUI工具里面的[ID值]名称，
如Button1,当返回值为false的时候系统将不再处理这个按键，返回true的时候系统将会继续处理此按键。比如SYS_BACK.
*========================onSlideWindowItemClick_XXXX(int index) 
当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如slideWindow1;index 代表按下图标的偏移值
*========================onSeekBarChange_XXXX(int progress) 
当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如SeekBar1;progress 代表当前的进度值
*========================ogetListItemCount_XXXX() 
当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[ID值]名称，
如List1;返回值为当前列表的总条数
*========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
 当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[ID值]名称，
如List1;pListItem 是贴图中的单条目对象，index是列表总目的偏移量。具体见函数说明
*========================常用接口===============
*LOGD(...)  打印调试信息的接口
*mTextXXX->setText("****") 在控件TextXXX上显示文字****
*mButton1->setSelected(true); 将控件mButton1设置为选中模式，图片会切换成选中图片，按钮文字会切换为选中后的颜色
*mSeekBar->setProgress(12) 在控件mSeekBar上将进度调整到12
*mListView1->refreshListView() 让mListView1 重新刷新，当列表数据变化后调用
*mDashbroadView1->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
*
* 在Eclipse编辑器中  使用 “alt + /”  快捷键可以打开智能提示
*/

#include "stringOperate.h"

static std::vector<string> gMediaFileList;
static int gPlayIndex = 0;

static bool parseFileList(char *pFileListPath, std::vector<string>& mediaFileList) {
	mediaFileList.clear();
	if (NULL == pFileListPath || 0 == strlen(pFileListPath)) {
		return false;
	}

	string tmp;
	char *bufLine = (char*) malloc(1024);
	char *pBuf;
	if (NULL == bufLine) {
		return false;
	}

	ifstream is(pFileListPath, ios_base::in);
	if (!is.is_open()) {
		goto _err0;
	}
	while (getline(is, tmp)) {
		removeCharFromString(tmp, '\"');
		removeCharFromString(tmp, '\r');
		removeCharFromString(tmp, '\n');
		mediaFileList.push_back(string(tmp.c_str()));
	}
	LOGD("(f:%s, l:%d) parse fileList[%s], get [%d]files\n", __FUNCTION__,
			__LINE__, pFileListPath, mediaFileList.size());
	for (size_t i = 0; i < mediaFileList.size(); i++) {
		LOGD("file[%d]:[%s]\n", i, mediaFileList[i].c_str());
	}
	free(bufLine);
	is.close();

	return true;

_err0: free(bufLine);
	bufLine = NULL;
	return false;
}

static BOOL sIsFullScreen = FALSE;
static bool sIsFirstSetVolume = true;
class VideoPlayerMessageListener : public ZKVideoView::IVideoPlayerMessageListener {
public:
	virtual void onVideoPlayerMessage(ZKVideoView *pVideoView, int msg) {
		LOGD("\n");
		switch (msg) {
		case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED:
			LOGD("ZKVideoView::E_MSGTYPE_VIDEO_PLAY_STARTED\n");
			if (mvideoplayerPtr) {
				if (mprogressPtr) {
					mprogressPtr->setMax(mvideoplayerPtr->getDuration());
					mprogressPtr->setProgress(mvideoplayerPtr->getCurrentPosition());
				}

				if (mplay_pausePtr) {
					mplay_pausePtr->setSelected(TRUE);
				}

				if (sIsFirstSetVolume) {
					sIsFirstSetVolume = false;
					mvideoplayerPtr->setVolume(0.5);
				}
			}
			break;
		case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED:
			LOGD("ZKVideoView::E_MSGTYPE_VIDEO_PLAY_COMPLETED\n");
			if (mvideoplayerPtr && !gMediaFileList.empty()) {
				gPlayIndex = (gPlayIndex + 1) % gMediaFileList.size();
				mvideoplayerPtr->play(gMediaFileList[gPlayIndex].c_str());
			}
			break;
		case ZKVideoView::E_MSGTYPE_VIDEO_PLAY_ERROR:
			break;
		}
	}
};
static VideoPlayerMessageListener sVideoPlayerMessageListener;

static bool gIsTracking = false;
static int gProgress = -1;

/**
 * 注册定时器
 * 在此数组中添加即可
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	{0,  1000}, //定时器id=0, 时间间隔6秒
	//{1,  1000},
};

static void onUI_init(){
    //Tips :添加 UI初始化的显示代码到这里,如:mText1->setText("123");
	parseFileList("/mnt/extsd/filelist.txt", gMediaFileList);

	if (mvideoplayerPtr) {
		mvideoplayerPtr->setVideoPlayerMessageListener(&sVideoPlayerMessageListener);
		if (!gMediaFileList.empty()) {
			mvideoplayerPtr->play(gMediaFileList[0].c_str());
		}
	}

	if (msound_progressPtr) {
		msound_progressPtr->setMax(10);
		msound_progressPtr->setProgress(5);
	}

	EASYUICONTEXT->setScreensaverEnable(false);
}

static void onUI_quit() {
	EASYUICONTEXT->setScreensaverEnable(true);

}

static bool onUI_Timer(int id){
    //Tips:添加定时器响应的代码到这里,但是需要在本文件的 REGISTER_ACTIVITY_TIMER_TAB 数组中 注册
    //id 是定时器设置时候的标签,这里不要写耗时的操作，否则影响UI刷新,ruturn:[true] 继续运行定时器;[false] 停止运行当前定时器
	switch (id) {
	case 0:
		if (!gIsTracking && mvideoplayerPtr && mprogressPtr) {
			mprogressPtr->setProgress(mvideoplayerPtr->getCurrentPosition());
		}
		break;
	}

    return true;
}

static bool onvideoActivityTouchEvent(const MotionEvent &ev) {
    // 返回false触摸事件将继续传递到控件上，返回true表示该触摸事件在此被拦截了，不再传递到控件上
    return false;
}

static void onProgressChanged_progress(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged progress %d !!!\n", progress);
	gProgress = progress;
}

static void onProgressStartTrackingTouch_progress(ZKSeekBar *pSeekBar) {
	gIsTracking = true;
}

static void onProgressStopTrackingTouch_progress(ZKSeekBar *pSeekBar) {
	gIsTracking = false;
	if (mvideoplayerPtr) {
		LOGD("onProgressStopTrackingTouch_progress gProgress: %d\n", gProgress);
		mvideoplayerPtr->seekTo(gProgress);
	}
}

static bool onButtonClick_play_pause(ZKButton *pButton) {
    //LOGD(" ButtonClick play_pause !!!\n");

	if (mvideoplayerPtr) {
		if (mvideoplayerPtr->isPlaying()) {
			mvideoplayerPtr->pause();
			pButton->setSelected(FALSE);
		} else {
			mvideoplayerPtr->resume();
			pButton->setSelected(TRUE);
		}
	}

    return true;
}

static bool onButtonClick_prev(ZKButton *pButton) {
    //LOGD(" ButtonClick prev !!!\n");
	if (mvideoplayerPtr && !gMediaFileList.empty()) {
		gPlayIndex = (gPlayIndex - 1 + gMediaFileList.size()) % gMediaFileList.size();
		mvideoplayerPtr->play(gMediaFileList[gPlayIndex].c_str());
	}
    return true;
}

static bool onButtonClick_next(ZKButton *pButton) {
    //LOGD(" ButtonClick next !!!\n");
	if (mvideoplayerPtr && !gMediaFileList.empty()) {
		gPlayIndex = (gPlayIndex + 1) % gMediaFileList.size();
		mvideoplayerPtr->play(gMediaFileList[gPlayIndex].c_str());
	}
    return true;
}

static bool onButtonClick_zoom(ZKButton *pButton) {
    //LOGD(" ButtonClick zoom !!!\n");
	static LayoutPosition fullScreenPosition(0, 0, 800, 480);
	static LayoutPosition smallScreenPosition(200, 60, 400, 250);
	sIsFullScreen = !sIsFullScreen;
	if (mvideoplayerPtr) {
		mvideoplayerPtr->setPosition(sIsFullScreen ? fullScreenPosition : smallScreenPosition);
	}
    return true;
}

static bool onButtonClick_sound(ZKButton *pButton) {
    //LOGD(" ButtonClick sound !!!\n");
	msound_WindowPtr->setVisible(TRUE);
    return true;
}

static bool onButtonClick_video(ZKButton *pButton) {
	if (mcontrol_windowPtr && sIsFullScreen) {
		if (mcontrol_windowPtr->isWndShow()) {
			mcontrol_windowPtr->hideWnd();
		} else {
			mcontrol_windowPtr->showWnd();
		}
	}
}

static void onProgressChanged_sound_progress(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged sound_progress %d !!!\n", progress);
	if (mvideoplayerPtr) {
		mvideoplayerPtr->setVolume((float) progress / 10);
	}
}

static void onProgressStartTrackingTouch_sound_progress(ZKSeekBar *pSeekBar) {

}

static void onProgressStopTrackingTouch_sound_progress(ZKSeekBar *pSeekBar) {

}
static bool onButtonClick_sys_back(ZKButton *pButton) {
    //LOGD(" ButtonClick sys_back !!!\n");
    return true;
}

static void onProtocolDataUpdate(const SProtocolData &data) {
    // 串口数据回调接口
}

